package cn.kgc.mes.storage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;

import cn.kgc.mes.type.service.IItemTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.kgc.mes.common.annotation.Log;
import cn.kgc.mes.common.core.controller.BaseController;
import cn.kgc.mes.common.core.domain.AjaxResult;
import cn.kgc.mes.common.enums.BusinessType;
import cn.kgc.mes.storage.domain.MaterialStock;
import cn.kgc.mes.storage.service.IMaterialStockService;
import cn.kgc.mes.common.utils.poi.ExcelUtil;
import cn.kgc.mes.common.core.page.TableDataInfo;

/**
 * 库存现有量Controller
 * 
 * @author ydw
 * @date 2025-09-11
 */
@RestController
@RequestMapping("/stock/stock")
public class MaterialStockController extends BaseController
{
    @Autowired
    private IMaterialStockService materialStockService;
@Autowired private IItemTypeService itemTypeService;
    /**
     * 查询库存现有量列表
     */
//    @PreAuthorize("@ss.hasPermi('stock:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialStock materialStock){
        //查询本id下所有子节点的id
        List<Long> itemTypeChildren = itemTypeService.selectItemTypeChildren(materialStock.getItemTypeId());
        materialStock.setItemTypeChildren(itemTypeChildren);
        System.out.println(itemTypeChildren);
        startPage();
        List<MaterialStock> list = materialStockService.selectMaterialStockList(materialStock);
        return getDataTable(list);
    }

    /**
     * 导出库存现有量列表
     */
//    @PreAuthorize("@ss.hasPermi('stock:stock:export')")
    @Log(title = "库存现有量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialStock materialStock)
    {
        List<MaterialStock> list = materialStockService.selectMaterialStockList(materialStock);
        ExcelUtil<MaterialStock> util = new ExcelUtil<MaterialStock>(MaterialStock.class);
        util.exportExcel(response, list, "库存现有量数据");
    }

    /**
     * 获取库存现有量详细信息
     */
//    @PreAuthorize("@ss.hasPermi('stock:stock:query')")
    @GetMapping(value = "/{materialStockId}")
    public AjaxResult getInfo(@PathVariable("materialStockId") Long materialStockId)
    {
        return success(materialStockService.selectMaterialStockByMaterialStockId(materialStockId));
    }

    /**
     * 新增库存现有量
     */
//    @PreAuthorize("@ss.hasPermi('stock:stock:add')")
    @Log(title = "库存现有量", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialStock materialStock)
    {
        return toAjax(materialStockService.insertMaterialStock(materialStock));
    }

    /**
     * 修改库存现有量
     */
//    @PreAuthorize("@ss.hasPermi('stock:stock:edit')")
    @Log(title = "库存现有量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialStock materialStock)
    {
        return toAjax(materialStockService.updateMaterialStock(materialStock));
    }

    /**
     * 删除库存现有量
     */
//    @PreAuthorize("@ss.hasPermi('stock:stock:remove')")
    @Log(title = "库存现有量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialStockIds}")
    public AjaxResult remove(@PathVariable Long[] materialStockIds)
    {
        return toAjax(materialStockService.deleteMaterialStockByMaterialStockIds(materialStockIds));
    }
    //新增库存量
    @PostMapping("/addMaterial")
    public Object addMaterial(@RequestBody MaterialStock materialStock){
        materialStock.setRecptDate(new java.util.Date());
        int i = materialStockService.insertMaterialStock(materialStock);
        Map<String,Object> map=new HashMap<>();
        if(i>0){
            map.put("code",200);
            map.put("msg","新增成功");
        }else{
            map.put("code",0);
            map.put("msg","新增失败");
        }
        return  map;
    }
    //根据itemId删除库存现有量
    @PostMapping("/deleteMaterialStockByItemId")
    public Object deleteMaterialStockByItemId(Long itemId){
        System.out.println("删除库存现有量");
        System.out.println(itemId);
        int i = materialStockService.deleteMaterialStockByItemId(itemId);
        Map<String,Object> map=new HashMap<>();
        if(i>0){
            map.put("code",200);
            map.put("msg","删除成功");
        }else{
            map.put("code",0);
            map.put("msg","删除失败");
        }
        return  map;
    }
}
